<?php
class Admin_Form_ValidateService extends Zendvn_Validate_Form{

	public function __construct($arrParam = null, $options = null){
		
		define('NAME_ID',$arrParam['id']);
		$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
												'fileConfig'=>'validates/service.ini'));
		if($arrParam['action']=='edit'){
			$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
					'fileConfig'=>'validates/service.ini',
					'section'=>'edit'));
		}
		//================================Validate Name================================
		$name  = $config->getConfig('name');
		$validator  =  $this->validate($name,$arrParam);
		if(!$validator->isValid($arrParam['name'])){
			$message = $validator->getMessages();
			$this->_arrError['name'] = current($message);
			$arrParam['name'] = '';
		}
		
		//================================Validate content================================
		$name  = $config->getConfig('content');
		$validator  =  $this->validate($name,$arrParam);
		if(!$validator->isValid($arrParam['content'])){
			$message = $validator->getMessages();
			$this->_arrError['content'] = current($message);
			$arrParam['content'] = '';
		}
	
		//================================Validate Html_descript================================
		if($arrParam['html_descript']){
			$name  = $config->getConfig('html_descript');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['html_descript'])){
				$message = $validator->getMessages();
				$this->_arrError['html_descript'] = current($message);
				$arrParam['html_descript'] = '';
			}
		}
		//================================Validate html_keyword================================
		if($arrParam['html_keyword']){
			$name  = $config->getConfig('html_keyword');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['html_keyword'])){
				$message = $validator->getMessages();
				$this->_arrError['html_keyword'] = current($message);
				$arrParam['html_keyword'] = '';
			}
		}
		
		/*=======================================================
		 * Start - validate picture
		*=======================================================*/
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo = $upload->getFileInfo('picture');
		$fileName = $fileInfo['picture']['name'];
		
		if(!empty($fileName)){
			$options = array('gif','png','jpg','jpeg');
			$upload->addValidator('Extension',true,$options,'picture')
			->addValidator('Size',true,array('min'=>'5kb','max'=>'10000kb'),'picture');
			// 					->addValidator('MimeType', false, array('image/gif','image/jpeg','image/png'));
			if(!$upload->isValid('picture')){
				$message = $upload->getMessages();
				$this->_arrError['picture'] = current($message);
				$arrParam['picture'] = '';
			}
		}else{
			if($arrParam["action"] == 'edit'){
				$arrParam['picture'] = $arrParam['current_picture'];
			}
		}
		
		/*=======================================================
		 * Start - return correct data array
		 *=======================================================*/
		$this->_arrData = $arrParam;
	}
	
	public function getData(){
		if(!$this->isVaild()){
			$arrFile = $this->upload();
			if(count($arrFile)>0){
				$this->_arrData = array_merge($arrFile,$this->_arrData);
			}
		}
		return $this->_arrData;
	}
	//return true|false
	public function isVaild(){
		$flag = false;
		if(count($this->_arrError) > 0 && $this->_arrError != ''){
			$flag = true;
		}
		return $flag;
	}
	
	public function getMessageErrors(){
		return $this->_arrError;
	}
	public function upload(){
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrData['module'],
				'fileConfig'=>'service.ini'));
		$categoryConfig = $config->getConfig('service');
		/*=======================================================
		 * Start - upload picture
		*=======================================================*/
	
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('picture');
		$fileName = $fileInfo['picture']['name'];
	
		if(!empty($fileName)){
			$options = array('custom_rename'=> true,'uploadDir'=> $categoryConfig['uploadDir']);
			$fileName = $upload->upload($categoryConfig['uploadDir'],'picture',$options);
			$arrFile['picture'] = $fileName;
			$arrParam = array(
					'uploadDir' => $categoryConfig['uploadDir'],
					'thumb-width' => array($categoryConfig['thumb-width'], $categoryConfig['thumb-width1'], $categoryConfig['thumb-width2'], $categoryConfig['thumb-width3']),
					'thumb-height' => array($categoryConfig['thumb-height'], $categoryConfig['thumb-height1'], $categoryConfig['thumb-height2'], $categoryConfig['thumb-height3']),
					'thumbDir'	=> array($categoryConfig['thumbDir'], $categoryConfig['thumbDir1'], $categoryConfig['thumbDir2'], $categoryConfig['thumbDir3'])
			);
			$options = array('type' => array($categoryConfig['thumbResize'], $categoryConfig['thumbResize1'], $categoryConfig['thumbResize2'], $categoryConfig['thumbResize3']));
			$img = Zendvn_File_Image::resizeImg($fileName, $arrParam, $options);
			if($this->_arrData['action'] == 'edit'){
				$upload->remove($categoryConfig['uploadDir'], $this->_arrData['current_picture']);
				$upload->remove($categoryConfig['thumbDir'], $this->_arrData['current_picture']);
				$upload->remove($categoryConfig['thumbDir1'], $this->_arrData['current_picture']);
				$upload->remove($categoryConfig['thumbDir2'], $this->_arrData['current_picture']);
				$upload->remove($categoryConfig['thumbDir3'], $this->_arrData['current_picture']);
			}
		}
		return $arrFile;
	}
}







